package com.hnhegui.backend.merchant.mapper;

import com.hnhegui.backend.merchant.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ProductMapper {

    /**
     * 根据商品ID查询商品
     */
    @Select("SELECT * FROM t_product WHERE product_id = #{productId}")
    Product findById(@Param("productId") Long productId);

    /**
     * 根据商品ID列表查询商品
     */
    @Select("<script>" +
            "SELECT * FROM t_product WHERE product_id IN " +
            "<foreach collection='productIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Product> findByIds(@Param("productIds") List<Long> productIds);

    /**
     * 查询商品列表（带分页和筛选）
     */
    @Select("<script>" +
            "SELECT * FROM t_product WHERE 1=1 " +
            "<if test='category != null and category != \"\"'>" +
            "AND category = #{category} " +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (product_name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='status != null'>" +
            "AND status = #{status} " +
            "</if>" +
            "ORDER BY " +
            "<if test='sortBy != null and sortBy != \"\"'>" +
            "#{sortBy} " +
            "<if test='sortOrder != null and sortOrder != \"\"'>" +
            "#{sortOrder} " +
            "</if>" +
            "</if>" +
            "<if test='sortBy == null or sortBy == \"\"'>" +
            "create_time DESC " +
            "</if>" +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<Product> findList(@Param("category") String category,
                           @Param("keyword") String keyword,
                           @Param("status") Integer status,
                           @Param("sortBy") String sortBy,
                           @Param("sortOrder") String sortOrder,
                           @Param("offset") Integer offset,
                           @Param("pageSize") Integer pageSize);

    /**
     * 统计商品数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM t_product WHERE 1=1 " +
            "<if test='category != null and category != \"\"'>" +
            "AND category = #{category} " +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (product_name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='status != null'>" +
            "AND status = #{status} " +
            "</if>" +
            "</script>")
    long count(@Param("category") String category,
               @Param("keyword") String keyword,
               @Param("status") Integer status);

    /**
     * 扣减库存（使用悲观锁）
     */
    @Update("UPDATE t_product SET stock = stock - #{quantity}, update_time = NOW() " +
            "WHERE product_id = #{productId} AND stock >= #{quantity}")
    int decreaseStock(@Param("productId") Long productId, @Param("quantity") Integer quantity);

    /**
     * 增加库存（回滚用）
     */
    @Update("UPDATE t_product SET stock = stock + #{quantity}, update_time = NOW() " +
            "WHERE product_id = #{productId}")
    int increaseStock(@Param("productId") Long productId, @Param("quantity") Integer quantity);

    /**
     * 锁定商品记录（FOR UPDATE）
     */
    @Select("SELECT * FROM t_product WHERE product_id = #{productId} FOR UPDATE")
    Product selectForUpdate(@Param("productId") Long productId);
}
